
let bodyParser = {
    json() {
        return function (req, res, next) {
            if (req.headers['content-type'] === 'application/json') {
                let arr = [];
                req.on('data', function (data) {
                    arr.push(data);
                });
                req.on('end', function () {
                    let str = Buffer.concat(arr).toString();
                    req.body = JSON.parse(str);
                    next();
                })
            } else {
                next();
            }

        }
    },
    urlencoded() {
        return function (req, res, next) {
            if (req.headers['content-type'] === 'application/x-www-form-urlencoded') {
                let arr = [];
                req.on('data', function (data) {
                    arr.push(data);
                });
                req.on('end', function () {
                    let str = Buffer.concat(arr).toString();
                    req.body = require('querystring').parse(str);
                    next();
                })
            } else {
                next();
            }

        }
    }
}

module.exports = bodyParser